package com.hrhx.springboot.web;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.math3.fitting.WeightedObservedPoints;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.json.GsonOption;
import com.hrhx.duhongming.bean.EchartsConfig;
import com.hrhx.duhongming.bean.EchartsHeatMap;
import com.hrhx.duhongming.bean.EchartsOnClick;
import com.hrhx.duhongming.core.EchartsCore;
import com.hrhx.duhongming.curve.PolynomialCurveCore;
import com.hrhx.duhongming.fel.FelCore;
import com.hrhx.springboot.utils.JsonUtil;

@Controller
@RequestMapping(value="/echarts")
public class EchartsDemo {
	
	/**
	 * 演示页面
	 * @return
	 */
	@RequestMapping(value="demo")
	public String echartsDemo(){
		return "/echarts/demo";
	}
	/**
	 * 获取参数到前台显示
	 * 也可以通过制定控制器去请求其他图表的URL
	 * @param echartsOnClick
	 * @return
	 */
	@RequestMapping(value="onclick")
	public String onclick(EchartsOnClick echartsOnClick,ModelMap modelMap){
		modelMap.put("echartsOnClick",echartsOnClick);
		return "/common/onclick";
	}
	
	/**
	 * 折线图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="line")
	public @ResponseBody String line(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterLineAndBar(echartsConfig,gsonOption,getLineData());
		System.out.println("测试传入参数："+echartsConfig.getParameters());
		return gsonOption.toString();
	}
	//测试数据请忽略
	private List<Map<String,Object>> getLineData(){
		//相当于二维数据表数据
		//x代表X轴的数据
		//y0代表Y轴左面的数据
		//y1代表Y轴右面的数据
		//x:日期           y0:最高气温          y1:最低气温
		//周一                11          11
		String jsonData="[{\"y1:最低气温\":null,\"y0:最高气温\":11,\"x:日期\":\"周一\"},{\"y1:最低气温\":-2,\"y0:最高气温\":11,\"x:日期\":\"周二\"},{\"y1:最低气温\":2,\"y0:最高气温\":15,\"x:日期\":\"周三\"},{\"y1:最低气温\":5,\"y0:最高气温\":13,\"x:日期\":\"周四\"},{\"y1:最低气温\":3,\"y0:最高气温\":12,\"x:日期\":\"周五\"},{\"y1:最低气温\":2,\"y0:最高气温\":13,\"x:日期\":\"周六\"},{\"y1:最低气温\":1,\"y0:最高气温\":10,\"x:日期\":\"周日\"}]";
		return new JsonUtil().getMapListData(jsonData);
	}
	
	/**
	 * 条形图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="bar")
	public @ResponseBody String bar(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterLineAndBar(echartsConfig,gsonOption,getBarData());
		return gsonOption.toString();
	}
	
	//测试数据请忽略
	private List<Map<String,Object>> getBarData(){
		//相当于二维数据表数据
		//x代表X轴的数据
		//y0代表Y轴左面的数据
		//x:日期           y0:最高气温          y0:最低气温
		//周一                11          11
		String jsonData="[{\"y0:最高气温\":11,\"y0:最低气温\":1,\"x:日期\":\"周一\"},{\"y0:最高气温\":11,\"y0:最低气温\":-2,\"x:日期\":\"周二\"},{\"y0:最高气温\":15,\"y0:最低气温\":2,\"x:日期\":\"周三\"},{\"y0:最高气温\":13,\"y0:最低气温\":5,\"x:日期\":\"周四\"},{\"y0:最高气温\":12,\"y0:最低气温\":3,\"x:日期\":\"周五\"},{\"y0:最高气温\":13,\"y0:最低气温\":2,\"x:日期\":\"周六\"},{\"y0:最高气温\":10,\"y0:最低气温\":1,\"x:日期\":\"周日\"}]";
		return new JsonUtil().getMapListData(jsonData);
	}
	
	/**
	 * 折线图条形图混合图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="barAndLine")
	public @ResponseBody String barAndLine(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterLineAndBar(echartsConfig,gsonOption,getLineData());
		//指定每条曲线的类型
		gsonOption.series().get(0).type(SeriesType.line);
		gsonOption.series().get(1).type(SeriesType.bar);
		return gsonOption.toString();
	}
	
	/**
	 * 饼状图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="pie")
	public @ResponseBody String pie(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterPie(echartsConfig,gsonOption,getPieData());
		//自定义配置
		gsonOption.title().x(X.center);
		gsonOption.legend().left(X.left).orient(Orient.vertical);
		return gsonOption.toString();
	}
	
	//测试数据请忽略
	private Map<String,Object> getPieData(){
		String jsonData="{\"搜索引擎\":1548,\"视频广告\":135,\"联盟广告\":234,\"邮件营销\":310,\"直接访问\":310}";
		return new JsonUtil().getMapData(jsonData);
	}
	
	/**
	 * 双数值折线图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="doubleNumLine")
	public @ResponseBody String doubleNumLine(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterDoubleNumLine(echartsConfig,gsonOption,getDoubleNumLineData());
		return gsonOption.toString();
	}
	
	/**
	 * 双对象折线图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="doubleObjLine")
	public @ResponseBody String doubleObjLine(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterDoubleNumLine(echartsConfig,gsonOption,getObjectNumLineData());
		return gsonOption.toString();
	}
	
	private Map<String,Object[][]> getObjectNumLineData(){
		Map<String,Object[][]> dateMap = new HashMap<>();
		Object data1[][] = new Object[][]{
			{"2017-06-01",38.8},{"2017-06-02",129.6},{"2017-06-03",259.9},{"2017-06-04",447.1},{new Date(),702.5}
		};
		dateMap.put("H82-2.0MW功率曲线", data1);
		Object data2[][] = new Object[][]{
			{"2017-06-01",8.2},{"2017-06-03",68.8},{"2017-06-04",187.3},{"2017-06-05",355.8},{"2017-06-06",597.2},
		};
		dateMap.put("H93-2.0MW功率模拟曲线", data2);
		return dateMap;
	}
	
	//测试数据请忽略
	private Map<String,Object[][]> getDoubleNumLineData(){
		Map<String,Object[][]> dateMap = new HashMap<>();
		
		Long currentTimeMillis = System.currentTimeMillis();
		Double data1[][] = new Double[][]{
			{4d,38.8},{5d,129.6},{6d,259.9},{7d,447.1},{8d,702.5},
			{9d,1015.2},{10d,1346.5},{11d,1665.1},{12d,1927.8},{12.4,2000d},
			{13d,2000d},{14d,2000d},{15d,2000d},{16d,2000d},{17d,2000d},{18d,2000d},
			{19d,2000d},{20d,2000d},{21d,2000d},{22d,2000d},{23d,2000d},{24d,2000d},{25d,2000d}
		};
		dateMap.put("H82-2.0MW功率曲线", data1);
		
		String expression1 = new PolynomialCurveCore().polynomialCurveExpresionAll(data1);
		Double d1[] = new Double[data1.length];
		for(int i=0;i<data1.length;i++){
			d1[i]=data1[i][0];
		}
        dateMap.put("H82-2.0MW功率模拟曲线", FelCore.getResult(expression1, d1));
       
		Double data2[][] = new Double[][]{
			{3d,8.2},{4d,68.8},{5d,187.3},{6d,355.8},{7d,597.2},
			{8d,916.4},{9d,1302.2},{10d,1699.5},{10.8,2000d},{11d,2000d},
			{12d,2000d},{13d,2000d},{14d,2000d},{15d,2000d},{16d,2000d},
			{17d,2000d},{18d,2000d},{19d,2000d},{20d,2000d},{21d,2000d},
			{22d,2000d},{23d,2000d},{24d,2000d},{25d,2000d},
		};
		dateMap.put("H93-2.0MW功率曲线", data2);
		WeightedObservedPoints weightedObservedPoints = new WeightedObservedPoints();
		for(int i=0;i<data2.length;i++){
			weightedObservedPoints.add(data2[i][0],data2[i][1]);
		}
		
		String expression2 = new PolynomialCurveCore().polynomialCurveExpresionAll(weightedObservedPoints);
		Double d2[] = new Double[data2.length];
		for(int i=0;i<data2.length;i++){
			d2[i]=data2[i][0];
		}
        dateMap.put("H93-2.0MW功率模拟曲线", FelCore.getResult(expression2, d2));
        System.currentTimeMillis();
        System.out.println("所用毫秒数："+(System.currentTimeMillis()-currentTimeMillis));
		return dateMap;
	}
	
	/**
	 * 反转条形图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="reverseBar")
	public @ResponseBody String reverseBar(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterReverseBar(echartsConfig,gsonOption,getBarData());
		return gsonOption.toString();
	}
	
	/**
	 * 雷达图
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="radar")
	public @ResponseBody String radar(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterRadar(echartsConfig,gsonOption,getRadarData());
		return gsonOption.toString();
	}
	
	//测试数据请忽略
	private List<Map<String,Object>> getRadarData(){
		List<Map<String,Object>> list = new ArrayList<>();
		//相当于二维数据表数据
		//x代表X轴的数据
		//y0代表Y轴左面的数据
		//y1代表Y轴右面的数据
		//x:name         y:预算分配（Allocated Budget）      y:实际开销（Actual Spending）  x:max
		//销售（sales）                      4300                     5000                      6500
		//管理（Administration）10000                   14000                     16000
		Map<String,Object> map1 = new HashMap<>();
		map1.put("x:name", "销售（sales）");
		map1.put("x:max", 6500);
		map1.put("y:预算分配（Allocated Budget）",4300);
		map1.put("y:实际开销（Actual Spending）", 5000);
		list.add(map1);
		
		Map<String,Object> map2 = new HashMap<>();
		map2.put("x:name","管理（Administration）");
		map2.put("x:max",16000);
		map2.put("y:预算分配（Allocated Budget）",10000);
		map2.put("y:实际开销（Actual Spending）", 14000);
		list.add(map2);
		
		Map<String,Object> map3 = new HashMap<>();
		map3.put("x:name", "信息技术（Information Techology）");
		map3.put("x:max", 30000);
		map3.put("y:预算分配（Allocated Budget）",28000);
		map3.put("y:实际开销（Actual Spending）", 28000);
		list.add(map3);
		
		Map<String,Object> map4 = new HashMap<>();
		map4.put("x:name", "客服（Customer Support）");
		map4.put("x:max",38000);
		map4.put("y:预算分配（Allocated Budget）",35000);
		map4.put("y:实际开销（Actual Spending）", 31000);
		list.add(map4);
		
		Map<String,Object> map5 = new HashMap<>();
		map5.put("x:name", "研发（Development）");
		map5.put("x:max", 52000);
		map5.put("y:预算分配（Allocated Budget）",50000);
		map5.put("y:实际开销（Actual Spending）", 42000);
		list.add(map5);
		
		Map<String,Object> map6 = new HashMap<>();
		map6.put("x:name", "市场（Marketing）");
		map6.put("x:max", 25000);
		map6.put("y:预算分配（Allocated Budget）",19000);
		map6.put("y:实际开销（Actual Spending）", 21000);
		list.add(map6);
		
		return list;
	}
	
	/**
	 * 地图图表
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="map")
	public @ResponseBody String map(@RequestBody EchartsConfig echartsConfig){
		//配置
		EchartsCore echartsCore = EchartsCore.getInstance();
		GsonOption gsonOption = echartsCore.getBaseGsonOption(echartsConfig);
		//数据适配器
		gsonOption = echartsCore.adapterMap(echartsConfig,gsonOption,getMapData());
		return gsonOption.toString();
	}
	
	//测试数据请忽略
	public Map<String,Map<String,Double>> getMapData(){
		Map<String,Map<String,Double>> map = new HashMap<>();
		
		Map<String,Double> iphone3 = new HashMap<>();
		iphone3.put("北京",randomData());
        iphone3.put("天津",randomData());
        iphone3.put("上海",randomData());
        iphone3.put("重庆",randomData());
        iphone3.put("河北",randomData());
        iphone3.put("河南",randomData());
        iphone3.put("云南",randomData());
        iphone3.put("辽宁",randomData());
        iphone3.put("黑龙江",randomData());
        iphone3.put("湖南",randomData());
        iphone3.put("安徽",randomData());
        iphone3.put("山东",randomData());
        iphone3.put("新疆",randomData());
        iphone3.put("江苏",randomData());
        iphone3.put("浙江",randomData());
        iphone3.put("江西",randomData());
        iphone3.put("湖北",randomData());
        iphone3.put("广西",randomData());
        iphone3.put("甘肃",randomData());
        iphone3.put("山西",randomData());
        iphone3.put("内蒙古",randomData());
        iphone3.put("陕西",randomData());
        iphone3.put("吉林",randomData());
        iphone3.put("福建",randomData());
        iphone3.put("贵州",randomData());
        iphone3.put("广东",randomData());
        iphone3.put("青海",randomData());
        iphone3.put("西藏",randomData());
        iphone3.put("四川",randomData());
        iphone3.put("宁夏",randomData());
        iphone3.put("海南",randomData());
        iphone3.put("台湾",randomData());
        iphone3.put("香港",randomData());
        iphone3.put("澳门",randomData());
		
		Map<String,Double> iphone4 = new HashMap<>();
		 iphone4.put("北京",randomData());
         iphone4.put("天津",randomData());
         iphone4.put("上海",randomData());
         iphone4.put("重庆",randomData());
         iphone4.put("河北",randomData());
         iphone4.put("安徽",randomData());
         iphone4.put("新疆",randomData());
         iphone4.put("浙江",randomData());
         iphone4.put("江西",randomData());
         iphone4.put("山西",randomData());
         iphone4.put("内蒙古",randomData());
         iphone4.put("吉林",randomData());
         iphone4.put("福建",randomData());
         iphone4.put("广东",randomData());
         iphone4.put("西藏",randomData());
         iphone4.put("四川",randomData());
         iphone4.put("宁夏",randomData());
         iphone4.put("香港",randomData());
         iphone4.put("澳门",randomData());
		
		Map<String,Double> iphone5 = new HashMap<>();
		
		 iphone5.put("北京",randomData());
         iphone5.put("天津",randomData());
         iphone5.put("上海",randomData());
         iphone5.put("广东",randomData());
         iphone5.put("台湾",randomData());
         iphone5.put("香港",randomData());
         iphone5.put("澳门",randomData());
		
		map.put("iphone3", iphone3);
		map.put("iphone4", iphone4);
		map.put("iphone5", iphone5);
		return map;
	}
	
	/**
	 * 日历图表
	 * @param echartsConfig
	 * @return
	 */
	@RequestMapping(value="calendar")
	public @ResponseBody List<Map<String,Object>> calendar(@RequestBody EchartsConfig echartsConfig){
		return getCalendarData();
	}
	
	/**
	 * echartsDate  工作          睡觉        娱乐
	 * 2017-05-01   10    8     6
	 * 2017-05-02   8     10    6
	 * @return
	 */
	//测试数据请忽略
	public  List<Map<String,Object>> getCalendarData(){
		List<Map<String,Object>> list = new ArrayList<>();
		for(int i=1;i<=31;i++){
			String dateDay="";
			if(i<10){
				dateDay = "0"+i;
			}else{
				dateDay = ""+i;
			}
			Map<String,Object> map = new HashMap<>();
			map.put("echartsDate", "2017-05-"+dateDay);
			map.put("工作", randomData()/100);
			map.put("睡觉", randomData()/100);
			map.put("娱乐", randomData()/100);
			list.add(map);
		}
		return list;
	}
	
	/**
	 * x                 y          z
	 * 2017-06-19     	工作票       	    00
	 * 2017-06-20                   工作票                      10
	 * 2017-06-20	           操作票                      11
	 * 2017-06-21                   操作票                      21
	 * @return
	 */
	@RequestMapping(value="scatterData")
	public @ResponseBody EchartsHeatMap scatterData(@RequestBody EchartsConfig echartsConfig){
		
		List<Map<String,Object>> list = new ArrayList<>();
		for(int i=10;i<=31;i++){
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("x", "2017-06-"+i);
			map1.put("y", "工作票");
			map1.put("z", (int)(randomData()/100));
			list.add(map1);
			
			Map<String,Object> map2 = new HashMap<>();
			map2.put("x", "2017-06-"+i);
			map2.put("y", "操作票");
			map2.put("z", (int)(randomData()/100));
			list.add(map2);
			
			Map<String,Object> map3 = new HashMap<>();
			map3.put("x", "2017-06-"+i);
			map3.put("y", "缺陷");
			map3.put("z", (int)(randomData()/100));
			list.add(map3);
			
			Map<String,Object> map4 = new HashMap<>();
			map4.put("x", "2017-06-"+i);
			map4.put("y", "工单");
			map4.put("z", (int)(randomData()/100));
			list.add(map4);
			
			Map<String,Object> map5 = new HashMap<>();
			map5.put("x", "2017-06-"+i);
			map5.put("y", "故障");
			map5.put("z", (int)(randomData()/100));
			list.add(map5);
		}

		EchartsHeatMap echartsHeatMap = new EchartsHeatMap(list);
		return echartsHeatMap;
		
	}
	
	
	private double randomData() {
	    return Math.round(Math.random()*1000);
	}
	
}
